<?php



namespace App\Services\Other\Proxies;

use App\Services\Other\Models\SearchRecord;
use App\Services\Other\Interfaces\SearchRecordServiceInterface;

class SearchRecordService implements SearchRecordServiceInterface
{
    public function exists(string $resourceType, int $resourceId): bool
    {
        return SearchRecord::query()->where('resource_id', $resourceId)->where('resource_type', $resourceType)->exists();
    }
 public function  findExistingIds(string $resourceType,array $resourceIds ){

     return SearchRecord::query()->whereIn('resource_id', $resourceIds)->where('resource_type', $resourceType)->pluck('resource_id')->toArray();
 }

    public function store(string $resourceType, int $resourceId, array $data): void
    {
        $data = array_merge($data, [
            'resource_type' => $resourceType,
            'resource_id' => $resourceId,
        ]);
        SearchRecord::create($data);
    }
    public function insert(string $resourceType, array $resourceIds, array $data): void
    {

        $data = array_map(function($item, $index) use ($resourceType, $resourceIds) {
            $item['resource_type'] = $resourceType;
            $item['resource_id'] = $resourceIds[$index] ?? null;
            return $item;
        }, $data, array_keys($data));
        SearchRecord::insert($data);
    }

    public function destroy(string $resourceType, int $resourceId): void
    {
        $searchRecord = SearchRecord::query()->where('resource_id', $resourceId)->where('resource_type', $resourceType)->first();
        if ($searchRecord) {
            $searchRecord->delete();
        }
    }

    public function update(string $resourceType, int $resourceId, array $data): void
    {
        $searchRecord = SearchRecord::query()->where('resource_id', $resourceId)->where('resource_type', $resourceType)->first();
        if ($searchRecord) {
            $searchRecord->fill($data)->save();
        }
    }

    public function search(string $keywords,int $shopId, int $page = 1, int $size = 10, $type = '')
    {
        if (!$type && !$shopId) {
            $query = SearchRecord::search($keywords)->get();
            $data =  $query->forPage($page,$size)->toArray();
            return [
                'data' => $data,
                'total' => $query->count(),
            ];
        }

        // 最多一百条数据
        $results = SearchRecord::search($keywords)->take(100)->get();

        $data = $results->filter(function ($item) use ($type,$shopId) {
            if (!$type && $shopId) {
                return $item['company_id'] === $shopId;
            }
            // 只传递 type 进行过滤
            if (!$shopId && $type) {
                return $item['resource_type'] === $type;
            }
            // 同时传递 company_id 和 type 进行过滤
            if ($type && $shopId) {
                return $item['resource_type'] === $type && $item['company_id'] === $shopId;
            }
            // 其他情况不进行过滤
            return true;
        })->toArray();

        $total = count($data);
        $chunks = array_chunk($data, $size);

        return [
            'total' => $total,
            'data' => $chunks[$page - 1] ?? [],
        ];
    }
}
